package com.wxxymaker.edu.controller;

import com.blade.ioc.annotation.Inject;
import com.blade.mvc.annotation.Path;
import com.blade.mvc.annotation.PathParam;
import com.blade.mvc.annotation.PostRoute;
import com.blade.mvc.ui.RestResponse;
import com.wxxymaker.edu.model.entity.PracticeResponse;
import com.wxxymaker.edu.model.entity.User;
import com.wxxymaker.edu.roles.StudentRoles;
import com.wxxymaker.edu.service.PracticeResponseService;
import com.wxxymaker.edu.utils.TokenUtils;

/**
 * @AUTHOR soft
 * @DATE 2018/10/15 11:05
 * @DESCRIBE
 */
@StudentRoles
@Path(value = "/course/pres/api", restful = true)
public class PracticeResponseHandler {

    @Inject
    private PracticeResponseService practiceResponseService;

    @PostRoute("/submit")
    public RestResponse submit(PracticeResponse response) {
        User user = TokenUtils.currentUser();
        response.setUserId(user.getId());
        PracticeResponse sqlResponse = null;
        if (response.getId()!=null&&response.getId()>0) {
            sqlResponse = practiceResponseService.resubmit(response);
        } else {
            sqlResponse = practiceResponseService.add(response);
        }
        if (sqlResponse != null) {
            return RestResponse.ok(sqlResponse);
        }
        return RestResponse.fail();
    }

    @PostRoute("/get/:pid")
    public RestResponse get_(@PathParam Integer pid) {
        User user = TokenUtils.currentUser();
        PracticeResponse byPidAndUid = practiceResponseService.findByPidAndUid(pid, user.getId());
        if (byPidAndUid != null) {
            return RestResponse.ok(byPidAndUid);
        }
        return RestResponse.fail();
    }
}
